package com.wisezone.studentmanager.utils;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WebContext {
	private static String prefix = "views/";
	private static String suffix = ".jsp";
	
	/**
	 * 转发
	 * @param url
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public static void foward(String url,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		foward(url, null, request, response);
	}
	/**
	 * 转发带参数
	 * @param url
	 * @param paramterMap
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public static void foward(String url,Map<String,Object> paramterMap,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		if(paramterMap != null){
			Set<String> setParameter = paramterMap.keySet();
			Iterator<String> it = setParameter.iterator();
			while(it.hasNext()){
				String key = it.next();
				Object value= paramterMap.get(key);
				request.setAttribute(key, value);
			}
		}
		
		request.getRequestDispatcher(prefix+url+suffix).forward(request, response);
	}
	/**
	 * 重定向
	 * @param url
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public static void redirect(String url,HttpServletRequest request, HttpServletResponse response) throws IOException{
		response.sendRedirect(prefix+url+suffix);
		
	}
}
